package com.foamya.minego

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.View
import android.view.View.OnClickListener
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView

class ResultDialog(context: Context) : Dialog(context), OnClickListener {

    private lateinit var closeBtn: ImageButton
    private lateinit var resultIv: ImageView
    private lateinit var levelLTv: TextView
    private lateinit var lIv: ImageView
    private lateinit var levelTv: TextView
    private lateinit var levelRTv: TextView
    private lateinit var homeBtn: ImageButton
    private lateinit var nextBtn: ImageButton
    private lateinit var retryBtn: ImageButton

    private lateinit var onBehaviorListener: OnBehaviorListener

    fun setOnBehaviorListener(onBehaviorListener: OnBehaviorListener) {
        this.onBehaviorListener = onBehaviorListener
    }

    interface OnBehaviorListener {
        fun home() // 返回主页
        fun next() // 下一关
        fun retry() // 重玩
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_result)
        instantVIew()
        initPage()
    }

    private fun instantVIew() {
        closeBtn = findViewById(R.id.dialog_result_btn_close)
        resultIv = findViewById(R.id.dialog_result_iv)
        levelLTv = findViewById(R.id.dialog_result_level_l)
        lIv = findViewById(R.id.dialog_result_l)
        levelTv = findViewById(R.id.dialog_result_level)
        levelRTv = findViewById(R.id.dialog_result_level_r)
        homeBtn = findViewById(R.id.dialog_result_btn_home)
        nextBtn = findViewById(R.id.dialog_result_btn_next)
        retryBtn = findViewById(R.id.dialog_result_btn_retry)

        closeBtn.setOnClickListener(this)
        homeBtn.setOnClickListener(this)
        nextBtn.setOnClickListener(this)
        retryBtn.setOnClickListener(this)
    }

    private fun initPage() {
        val window = window
        window?.setBackgroundDrawableResource(android.R.color.transparent) // 背景色设为透明

        val level = BoxUtil.level // 获取关卡
        val previousLevel = level - 1 // 上一关
        val nextLevel = level + 1 // 下一关
        if (level == 1) { // 关卡等于1时不展示上一关
            levelLTv.visibility = View.INVISIBLE
            lIv.visibility = View.INVISIBLE
        } else { // 否则显示
            levelLTv.visibility = View.VISIBLE
            lIv.visibility = View.VISIBLE
            // 写入关卡
            levelLTv.text = "$previousLevel"
        }
        levelTv.text = "$level"
        levelRTv.text = "$nextLevel"


        if (BoxUtil.isWin == 1) { // 根据游戏结果判断展示的图片及下一关是否展示
            resultIv.setImageResource(R.mipmap.result_success)
            nextBtn.visibility = View.VISIBLE
            retryBtn.visibility = View.GONE
            BoxUtil.level++ // 游戏成功则关卡增加
        } else if (BoxUtil.isWin == 0) {
            resultIv.setImageResource(R.mipmap.result_fail)
            nextBtn.visibility = View.GONE
            retryBtn.visibility = View.VISIBLE
        }
    }

    override fun onClick(p0: View?) {
        cancel() // 取消弹窗
        when (p0?.id) {
            R.id.dialog_result_btn_close -> {}

            R.id.dialog_result_btn_home -> {
                onBehaviorListener.home()
            }

            R.id.dialog_result_btn_next -> {
                onBehaviorListener.next()
            }

            R.id.dialog_result_btn_retry -> {
                onBehaviorListener.retry()
            }
        }
    }
}